package com.czy.contorller.admin;

import com.czy.dto.UserDto;
import com.czy.dto.UserPageDto;
import com.czy.result.Result;
import com.czy.service.UserService;
import com.czy.vo.PageVo;
import com.czy.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminUserController {
    @Autowired
    private UserService userService;


    //分页查询用户信息
    @GetMapping("/list")
    public Result<PageVo> userPage(UserPageDto pageDto) {
        log.info("用户分页条件：{}", pageDto);
        PageVo page = userService.page(pageDto);
        return Result.success(page);
    }

    //新增用户
    @PostMapping("/add")
    public Result addUser(@RequestBody UserDto userDto) {
        log.info("新增用户信息：{}", userDto);
        userService.add(userDto);
        return Result.success();
    }

    //更新用户
    @PutMapping("/update/{id}")
    public Result updateUser(@RequestBody UserDto userDto) {
        log.info("修改数据为：{}", userDto);
        userService.update(userDto);
        return Result.success();
    }

    //批量删除用户
    @DeleteMapping("/delete")
    public Result deleteUser(@RequestParam List<Integer> ids) {
        log.info("根据id批量删除员工：{}", ids);
        userService.deleteByIdUser(ids);
        return Result.success();
    }

    //根据Id查询用户
    @GetMapping("/detail/{id}")
    public Result<UserVo> findById(@PathVariable Integer id) {
        log.info("要查询的用户id：{}", id);
        UserVo userVo = userService.findById(id);
        return Result.success(userVo);
    }
}
